package kd.bos.kdec.pincar;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
 * i拼车订单支付
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobPayForBill extends AbstractFormPlugin {

	private String[] buttons = new String[] { "finishpay" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		IFormView view = this.getView();
		FormShowParameter parameter = this.getView().getFormShowParameter();
		if (StringUtils.equals("finishpay", key)) {// 支付完成跳评价
			Map<String, Object>custs = parameter.getCustomParams();
			// 回写订单状态
			Long billPk = (Long) custs.get("pk");
			QFilter filter = new QFilter("billpkid", QCP.equals, billPk);
			DynamicObject objOrder = BusinessDataServiceHelper.loadSingleFromCache("kdec_pincarorder",
					"orderstatus,id,creator", filter.toArray());
			objOrder.set("orderstatus", 3);
			SaveServiceHelper.save(new DynamicObject[] { objOrder });
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", "kdec_pincar_evaluatemob");
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			// 传参为了头像+订单评价回写,暂时未为登录人员,后续用基础资料维护司机信息
			showParameter.setCustomParam("driver", custs.get("driverid"));
			view.showForm(showParameter);
		} 
	}

	@Override
	public void afterBindData(EventObject e) {
		IDataModel model = this.getModel();
		// 查订单人数
		FormShowParameter parameter = this.getView().getFormShowParameter();
		Map<String, Object> customParams = parameter.getCustomParams();
		long driverid = (long)customParams.get("driverid");
		Label ordercost = this.getControl("ordercost");
		ordercost.setText(customParams.get("ordercost").toString());
		
		model.setValue("driverfield", driverid);
		QFilter filter = new QFilter("creator", QCP.equals, driverid);
		DynamicObject obj = BusinessDataServiceHelper.loadSingleFromCache("kdec_pincardetailbill",
				"telephone,carbrand,cartype,carcolor,carnum,name", filter.toArray());
		Label name = this.getControl("name");
		name.setText(obj.getString("name"));
		Label tel = this.getControl("telephone1");
		tel.setText("手机号：" + obj.getString("telephone"));
		Label carnum = this.getControl("carnum");
		carnum.setText(obj.getString("carnum"));
		Label cardata = this.getControl("cardata");
		cardata.setText(obj.getString("carcolor") + "·" + obj.getString("carbrand") + " "
				+ obj.getString("cartype") + " ");
	}
}
